﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

// Basic global alias for the main class containing constants
using r = Rougeliek.Rougeliek;

namespace Rougeliek
{
    class Map
    {
        // Make some lovely new list items!
        public static List<Grass> foliage = new List<Grass>();
        public static List<Tree> trees = new List<Tree>();

        // Initialise the collision grid, this is the catch all boolean 2d array to check for collisions
        public static bool[,] collisionGrid;

        // This generates the map, duh!
        public void generateMap(int foliageCount, int treeCount)
        {
            // Just here for when resetting the map ingame, clear them out
            foliage.Clear();
            trees.Clear();

            // For the interest of avoiding out of bounds errors, set the
            // collision grid way bigger than necessary. Better safe than sorry
            collisionGrid = new bool[r.SCREENX+10, r.SCREENY+10];
            int x, y;
            Random rdm1 = new Random();

            // Add new grass items to the list based on the FOLIAGE constant
            for (int i = 0; i < (foliageCount); i++)
            {
                x = rdm1.Next(r.SCREENMIN, r.SCREENX);      // Randomly generate x and
                y = rdm1.Next(r.SCREENMIN, r.SCREENY);      // y coordinates
                foliage.Add(new Grass(x, y, rdm1.Next(2))); // Shove that bitch inna list!
            }

            // Add new tree items based on the TREES constant
            for (int i = 0; i < treeCount; i++)
            {
                x = rdm1.Next(r.SCREENMIN, r.SCREENX);
                y = rdm1.Next(r.SCREENMIN, r.SCREENY);
                trees.Add(new Tree(x, y));
            }                                               

        }

        // Simple collision detection, basically checks the collision array and returns true or false 
        // depending on it's contents
        public static bool checkCollision(int x, int y)
        {
            return (collisionGrid[x, y] == true) ? true : false;
        }
    }

    // Grass class for foliage background layer
    public class Grass
    {
        public int grassX, grassY;
        public char grassSprite;

        public Grass(int x, int y, int sprite)
        {
            grassX = x;
            grassY = y;
            // Randomise sprite based on input
            grassSprite = (sprite == 1) ? '\'' : '.';
        }
    }

    // Tree class for background layer, defined as solid when loaded into
    // the screenBuffer list
    public class Tree
    {
        public int treeX, treeY;
        public char treeSprite;

        public Tree(int x, int y)
        {
            treeX = x;
            treeY = y;
            treeSprite = '♣';
        }
    }
}
